<?php


namespace Modules\Inspection\Http\Repositories;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Modules\Inspection\Models\Inspectioncheck;
use Modules\Inspection\Models\Inspection;
use Illuminate\Support\Facades\DB;
use Catch\Exceptions\FailedException;

class InspectionReget
{
    /*
    数据列表
    */
    public static function list($data = [])
    {
        return self::getBuilder($data)->paginate($data['per_page'] ?? 20);
    }


     /**
     *查询列表
     *
     * @param array $data
     * @return Builder
     */
    public static function getBuilder(array $data = []): Builder
    {
       
        $builder = Inspection::query()->with('detailscheckAll');
        if (isset($data['inspectionnumber'])) {
            $builder->where('inspectionnumber', 'like', "%{$data['inspectionnumber']}%");
        }

        if (isset($data['productname'])) {
            $fnumber = DB::table('material')
            ->select('id','fnumber','fname','fspecification','funit_number','funit_name')
            ->where('fnumber',$data['productname'])
            ->first();
            if(!empty($fnumber)){
                // $builder->whereIn('productname',[$fnumber->id]);
                $builder->where('productname', 'like', "%{$fnumber->id}%");
            }else{
                $builder->where('id',$data['productname']);
            }
        }
        
        $builder->orderBy('id', 'desc');

        return $builder;
    }

    /*
    数据添加方法
    */
    public static function create(array $data)
    {
        
        $Inspection = new Inspection;
        return self::createOrSave($Inspection, $data);
    }

    //数据添加方法
    public static function createOrSave(Inspection $Inspection,$data)
    {
        try {
            //开始起事务
            DB::beginTransaction();
            $InspectionData = [];
            $InspectionData['productname'] = $data['productname'];
            $InspectionData['inspectionnumber'] = makeSerial();
            $InspectionData['completecheck'] = $data['completecheck'];
            $InspectionData['creator_id'] = $data['creator_id'];
            $Inspection->fill($InspectionData);
            $Inspection->save();
           
            $Inspection->detailscheckAll()->createMany($data['detailscheck_all']);
            DB::commit();
            return '数据添加成功';
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException($e);
        }
    }
    
    //修改
    public static function createUpdate($id,$data)
    {

        try {
            //开始起事务
            DB::beginTransaction();
            $Inspection = Inspection::query()->updateOrCreate(['id' => $id], [
                'productname'       => $data['productname']  ?? '',
                'completecheck'       => $data['completecheck']  ?? '',
            ]);

            Inspectioncheck::where('inspection_id',$id)->delete();
            $Inspection->detailscheckAll()->createMany($data['detailscheck_all']);
            DB::commit();
            return '修改成功';
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException($e);
        }
    }

    /**
     * 查询一条数据
     *
     */
    public static function find($id)
    {
        return Inspection::query()->with('detailscheckAll')->find($id);
    }

    /**
     * @param $id
     * @return void
     */
    public static function delete($id,$data)
    {
       
        try {
            //开始起事务
            DB::beginTransaction();
            $Inspection = Inspection::query()->findOrFail($id);
            $Inspection->detailscheckAll()->delete();
            $Inspection->delete();
            DB::commit();
            return '删除成功';
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException($e);
        }


    }

}
